#include "UdpServer.hpp"
#include "Comm.hpp"
#include <iostream>
#include <vector>
#include <memory>
#include <cstdio>

std::string OnMassageDefault(std::string massage)
{
    return massage + "  [haha , got you !!!]";
}

std::vector<std::string> black_words = {
    "rm",
    "unlink",
    "cp",
    "mv",
    "chmod",
    "exit",
    "reboot",
    "halt",
    "shutdown",
    "top",
    "kill",
    "dd",
    "vim",
    "vi",
    "nano",
    "man"
};

bool SafeCheck(std::string command)
{
    for (auto &str : black_words)
    {
        if (str.find(command) != std::string::npos)
            return false;
    }
    return true;
}

std::string RemoteExecution(std::string command)
{
    if (!SafeCheck(command))
        return "bad man !!!!";

    FILE *fp = popen(command.c_str(), "r");
    if (fp == nullptr)
    {
        return "execute error, reason is unknown";
    }

    std::string response;
    char buffer[1024];
    while(true)
    {
        int n = fread(buffer,1, sizeof(buffer) , fp);
        if(n <= 0) break;
        response+=buffer;
    }

    fclose(fp);
    return response;
}

void Usage(std::string proc)
{
    std::cout << "Usage : \n\t" << proc << " local_port" << std::endl;
    exit(Usage_Err);
}

int main(int argc, char *argv[])
{

    // ./UdpServer 0.0.0.0 8888
    if (argc != 2)
    {
        Usage(argv[0]);
    }
    uint16_t port = std::atoi(argv[1]);
    // std::shared_ptr<UdpServer> usvr = std::make_shared<UdpServer>(ip,port);
    std::shared_ptr<UdpServer> usvr = std::make_shared<UdpServer>(port);
    usvr->Init();
    usvr->Start();
    return 0;
}